﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_mroles : System.Web.UI.Page
{
    rUser ruser;
    DataSet perfiles, otrosPerfiles;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null) { Response.Redirect("../Default.aspx"); }

        restricted();
        permiso();
        //Si es la primera vez que entra
        if (!Page.IsPostBack)
        {
            TabContainer1.ActiveTab = TabPanel1;
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            ruser = (rUser)Session["userid"];
            //perfiles = (DataSet)Session["Perfiles"];
            string per = service.getPerfiles(1,1);
            perfiles = Utility.getPerfiles(per);
            Session["Perfiles"] = perfiles;
            gv_roles.DataSource = perfiles;
            gv_roles.DataBind();

            string per2 = service.getPerfiles(0,1);
            otrosPerfiles = Utility.getPerfiles(per2);
            Session["otrosPerfiles"] = otrosPerfiles;
            gv_otrosPerfiles.DataSource = otrosPerfiles;
            gv_otrosPerfiles.DataBind();
        }

    }
    protected void gv_roles_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        perfiles = (DataSet)(Session["Perfiles"]);

        if (perfiles != null)
        {
            gv_roles.PageIndex = e.NewPageIndex;
            gv_roles.DataSource = perfiles.Tables["Tabla"];
            gv_roles.DataBind();
        }
    }
    protected void gv_roles_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.CssClass = "letraNegra13";
            e.Row.Cells[0].Text = "Edit Role";
            e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[0].Width = 130;
            e.Row.Cells[1].Style.Add("display", "none"); // Hide
            e.Row.Cells[2].Text = "Role";
            e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[2].Width = 300;
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Style.Add("display", "none"); // Hide
            e.Row.CssClass = "letraNegra13";
        }
    }

    public void restricted()
    {
        long permiso = 0;
        rUser user = (rUser)(Session["userid"]);
        if (user != null)
        {
            //permiso = long.Parse(user.getRestricted().ToString());
            RE_GenericBean permi = Utility.getPermisobyRuser(user, 12);
            permiso = permi.int64C2;
        }
        if ((permiso & 1) == 1) { Response.Redirect(Request.UrlReferrer.ToString()); }
    }

    public void permiso()
    {
        long permiso = 0;
        rUser user = (rUser)(Session["userid"]);
        if (user != null)
        {
            permiso = long.Parse(user.getPermisoUser().ToString());
            RE_GenericBean permi = Utility.getPermisobyRuser(user, 12);
            permiso = permi.int64C1;
        }
        if ((permiso & 1) != 1) { gv_roles.Enabled = false; tb_role.Enabled = false; lnk_add.Enabled = false; }
    }
    protected void lnk_add_Click(object sender, EventArgs e)
    {
        //primero obtengo el valor de perfil que toca
        Label1.Text = "";
        com.bpophotos.www.Service service = new com.bpophotos.www.Service();

        string sxml = "";
        sxml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> <role><name>";
        sxml += tb_role.Text + "</name>";
        //le pongo defaul permiso de auto emialphotos y de ver Wk
        sxml += "<permiso>0</permiso>";
        sxml += "<app>1</app>";
        sxml += "</role>";
        int p = service.registraPerfil(sxml);
        if (p == 1)
        {
            Label1.Text = "Role " + tb_role.Text + " added ";
            tb_role.Text = "";
            string perfiles = service.getPerfiles(1,1);
            DataSet newperfiles = Utility.getPerfiles(perfiles);
            gv_roles.DataSource = newperfiles;
            gv_roles.DataBind();
            Session["Perfiles"] = newperfiles;
        }
    }
   
    protected void gv_otrosPerfiles_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.CssClass = "letraNegra13";
            e.Row.Cells[0].Text = "Edit Profile";
            e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[0].Width = 130;
            e.Row.Cells[1].Style.Add("display", "none"); // Hide
            e.Row.Cells[2].Text = "Profile";
            e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Left;
            e.Row.Cells[2].Width = 300;
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Style.Add("display", "none"); // Hide
            e.Row.CssClass = "letraNegra13";
        }
    }
    protected void gv_otrosPerfiles_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        otrosPerfiles = (DataSet)(Session["otrosPerfiles"]);

        if (otrosPerfiles != null)
        {
            gv_otrosPerfiles.PageIndex = e.NewPageIndex;
            gv_otrosPerfiles.DataSource = otrosPerfiles.Tables["Tabla"];
            gv_otrosPerfiles.DataBind();
        }
    }
}